<?php
$objXajax->registerFunction('frmaccountag_loadGrid');
$objXajax->registerFunction('frmaccountag_edit');
$objXajax->registerFunction('frmaccountag_save');
$objXajax->registerFunction('frmaccountag_init');
function frmaccountag_init()// tùy theo loại quyền mà load các quyền lên cbb khác nhau.
{ 
    global $lang,$path,$arrpowerview,$objDbSelect;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax

    $powertype_id = $_SESSION['current']['power_type_id'];
    $strpowerview = $arrpowerview[$powertype_id];
    $sqlselect = "select id,name_$lang as name from tbl_power_type where id in  ($strpowerview)";
    $arrPower = $objDbSelect->GetArray($sqlselect);
 
    if(is_array($arrPower)&& count($arrPower)>0){
        $oTbs =  new clsTinyButStrong();
        $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/main_accountag_acctype.html');
        $oTbs->MergeBlock('blk_category', $arrPower);
        $oTbs->Show(TBS_NOTHING);
        $objResponse->addAssign('frmaccountag_tdaccountype', 'innerHTML', $oTbs->Source);                
    }
    
    $sqlselect = "select id,username as name from tbl_user where power_type_id = ".POWER_AGENCY;
    $arrAgent = $objDbSelect->GetArray($sqlselect);
    
    if(is_array($arrAgent)&& count($arrAgent)>0){
        $oTbs1 =  new clsTinyButStrong();
        $oTbs1->LoadTemplate('templates/'.$lang.'/'.$path.'/main_accountag_cbbagent.html');
        $oTbs1->MergeBlock('blk_agent', $arrAgent);
        $oTbs1->Show(TBS_NOTHING);
        $objResponse->addAssign('frmaccountag_tdagent', 'innerHTML', $oTbs1->Source);                
    }
    if($powertype_id==POWER_MASTER||$powertype_id==POWER_SUPERVISOR||$powertype_id==POWER_ACCOUNT||$powertype_id==POWER_SUPPORTER){
    $objResponse->addScript("$('#frmaccountag_traccountype').show();");                        
                    
        
    }
    

   $objResponse->addScriptCall("xajax_frmaccountag_loadGrid();");     
   $objResponse->addScript('changeWaiting(1);');                        
   return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function frmaccountag_loadGrid($params=array())
{ 
    //echo '<pre>'; print_r($params); die();
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = frmaccountag_getGrid($params);  
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/main_accountag_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmaccountag_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmaccountag_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function frmaccountag_getGrid($params)
{
    global $objDbSelect,$hotel_id,$user_id;
    //echo '<pre>'; print_r($params); die();
           $power_type_id = $_SESSION['current']['power_type_id'];
           $addcondition ='';
           $power_id = addslashes($params['power_type_id']);
           $username = addslashes($params['user_name']);
           $phone =addslashes($params['phone']);
           if($power_id!=''){
              $addcondition .= " and power_type_id = $power_id ";
           }           
           if($username!=''){
              $addcondition .= " and username LIKE '%$username%' ";
           }           
           if($phone!=''){
              $addcondition .= " and phone LIKE '%$phone%' ";
           }
/*           $addcondition .= ($power_id=='')?'': " and power_type_id = $power_id ";
           $addcondition .= ($username=='')?'': " and username LIKE '%$username%' ";
           $addcondition .= ($phone=='')?'': " and phone LIKE '%$phone%' ";  */
           $sql ='';
           $power_type_id; 
           //echo $addcondition; die();
           switch($power_type_id){
               case POWER_MASTER:
               case POWER_SUPERVISOR:
               case POWER_ACCOUNT:
               $sql = " select * from tbl_user where power_type_id>$power_type_id $addcondition and power_type_id <>".POWER_CUSTOMER.
               " and power_type_id <> ".POWER_SUB_CUSTOMER; 
               break;
               case POWER_AGENCY: 
               $sql = " select * from tbl_user where user_created = $user_id $addcondition and power_type_id = ".POWER_CUSTOMER  ;
               break;
           } 
         // echo $sql; 
    return $objDbSelect->GetArray($sql);
} 

function frmaccountag_edit($id){
        global $objDbSelect;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT * FROM tbl_user WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        
        $email        = addslashes($arr[0]['email']);
        $address   = addslashes($arr[0]['address']);    
        $phone     = addslashes($arr[0]['phone']);
        $username     = addslashes($arr[0]['username']);
        
        $objResponse->addScript("$('#frmaccountag_txtEmail').val('$email');");
        $objResponse->addScript("$('#frmaccountag_txtAddress').val('$address');");
        $objResponse->addScript("$('#frmaccountag_txtPhone').val('$phone');");
        $objResponse->addScript("$('#frmaccountag_txtUsername').val('$username');");       
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
    function frmaccountag_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse =  new xajaxResponse();  
    $email =addslashes($params['txtEmail']) ;
    $address =addslashes($params['txtAddress']) ;
    $phone =addslashes($params['txtPhone']) ;
       
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit!=1){
      $objResponse->addScript('frmaccountag_alertSelect();');  
    }else{
       $sql ="UPDATE tbl_user SET email = '$email',address = '$address',phone = '$phone',
             user_update = '$user_id',date_update = now() WHERE id =$id"; 
        $objDbSelect->Execute($sql); 
        $objResponse->addScript('changeWaiting(1);');
        $objResponse->addScript('xajax_frmaccountag_loadGrid();');     
    }
    return $objResponse->getXML();
} 
?>
